package com.itheima.mq;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.RecommendMovement;
import com.itheima.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class RecommendMovementTask {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 1.读取redis的数据
     *  规则 key = QUANZI_PUBLISH_RECOMMEND_推给谁
     *      value =100092,18,20,1,20,22,23,25,24,10020  推荐的pid 动态id
     * 2.根据pid查询mongo的movement_detail表 读取出来movement对象
     * 3.将movement转换成recommend_movement 数据
     */
    @Scheduled(cron = "0/5 * * * * *")//时间的定义 看需求
    public void run(){
        System.out.println("启动定时任务读取redis数据 加载到mongo中");
        //1.读取redis的数据  (keys *  读取redis中所有的匹配的key)
        Set<String> keys = stringRedisTemplate.keys(ConstantUtil.QUANZI + "*");
        //判断key必须是存在的
        if(CollectionUtil.isNotEmpty(keys)){
            //遍历
            for (String key : keys) {
                //读取value数据  value =100092,18,20,1,20,22,23,25,24,10020
                String value = stringRedisTemplate.opsForValue().get(key);

                //需要切割数据
                String[] pids = value.split(",");


                if(pids!= null){
                    //获得用户id
                    Long userId =Long.valueOf( key.replaceAll(ConstantUtil.QUANZI , "") ); //去掉前缀
                    //1.删除原来的数据mongo 2.删除redis (保留mongo数据)  根据需求来
                    Query removeQuery = new Query(
                            Criteria.where("userId").is(userId)
                    );
                    //删除历史数据
                    mongoTemplate.remove(removeQuery , RecommendMovement.class);
                    //循环数据添加新的数据
                    for (String pid : pids) {
                        Query query = new Query(
                                Criteria.where("pid").is(Long.valueOf(pid))
                        );
                        //2.根据pid查询mongo的movement_detail表 读取出来movement对象
                        //movement.getUserId() 发布动态人的id
                        Movement movement = mongoTemplate.findOne(query, Movement.class);

                        //3.将movement转换成recommend_movement 数据
                        RecommendMovement recommendMovement = new RecommendMovement();
                        recommendMovement.setCreated(System.currentTimeMillis());//时间

                        recommendMovement.setUserId(userId); //给谁推
                        recommendMovement.setPid(Long.valueOf(pid)); //动态的数字id
                        recommendMovement.setPublishId( movement.getId() );//动态的id
                        //有坑, 大数据这边推送的数据的时候 少传一个分值 (后续跟大数据沟通)
                        recommendMovement.setScore(RandomUtil.randomDouble(60,99)); //暂时只能自己随机

                        mongoTemplate.save(recommendMovement);
                    }
                }

            }
        }
    }
}
